package com.Servlet;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.Order.FinishState;
import com.Order.Order;
import com.Order.OrderInquire;
import com.Order.SubmitState;
import com.Order.VerifyState;
import com.Order.WashinngState;

/**
 * Servlet implementation class OrderInt
 */
@WebServlet("/Order")
public class OrderInt extends HttpServlet{
	private static final long serialVersionUID = 1L;
    
    /**
     * @see HttpServlet#HttpServlet()
     */
    public OrderInt() {
        super();
        // TODO Auto-generated constructor stub
    }

    protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
    	long startTime   = System.currentTimeMillis(); 
    	
    	request.setCharacterEncoding("utf-8"); 
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/json");
		
		String method = request.getParameter("method");
		System.out.println("method:" + method);
		
    	// 获取请求string
		InputStreamReader reader=new InputStreamReader((ServletInputStream)request.getInputStream(),"utf-8");
        BufferedReader br = new BufferedReader(reader);
        StringBuffer sb = new StringBuffer("");
        String temp;
        while((temp = br.readLine()) != null){
            sb.append(temp);
        }
        br.close();
        
        String requestString=sb.toString();
        //requestString= "{'loginName':'金振明','password':'jinzhenming','userIdentity':3}";
		// 返回结果字符串
        String reString=null;
        Order order=new Order();
		switch (method) {
		// 查询
		case "getOrder":
			OrderInquire orderInquire=new OrderInquire();
			reString=orderInquire.getOrder(requestString);
			break;
		// 提交订单
		case "submit":
			SubmitState sumState=new SubmitState();
			reString=sumState.submitNewOrders(requestString);
			break;
		// 用户确认
		case "StudentConfirm":
			VerifyState verifyState=new VerifyState();
			reString=verifyState.VerifyOrder(requestString);
			break;
		// 用户撤回
		case "StudentWithdraw":
			VerifyState verifyState2=new VerifyState();
			reString=verifyState2.studentWithdraw(requestString);
			break;
		// 付钱
		case "StudentPay":
			VerifyState verifyState3=new VerifyState();
			reString=verifyState3.Payed(requestString);
			break;
		// 提前结束
		case "WashOver":
			WashinngState washinngState=new WashinngState();
			reString=washinngState.FinishEarly(requestString);
			break;
		default:
			break;
		}
		//System.out.print(requestString);
		PrintWriter out = response.getWriter();
		out.write(reString.replace("\"", "'"));
		out.close();
		long endTime   = System.currentTimeMillis(); //程序结束记录时间
		System.out.println(endTime - startTime); 
	}

}
